package com.minyifang.wcspf.work.ui.user.presenter;

import com.minyifang.wcspf.common.base.BasePresenter;
import com.minyifang.wcspf.common.config.network.Constants;
import com.minyifang.wcspf.common.config.network.RetrofitHelper;
import com.minyifang.wcspf.work.ui.user.model.CaptchaBean;
import com.minyifang.wcspf.work.model.HttpRespond;
import com.minyifang.wcspf.work.ui.user.view.ForgetPwdView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by LuoYaWei on 2018/6/14.
 */
public class ForgetPwdPresenter extends BasePresenter<ForgetPwdView> {
    public void getCaptchaUrl(String strPhone) {

        Map<String, Object> maps = new HashMap<>();
        maps.put("mobile", strPhone);
        maps.put("type", 1);
        JSONObject jsonData = Constants.getJsonData(maps, null);

        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getCaptcha(requestBody), captchaBeanHttpRespond -> getView().showCaptchaUrl(captchaBeanHttpRespond));
    }

    public void sendSMS(String phone, String captcha) {
        Map<String, Object> maps = new HashMap<>();
        maps.put("mobile", phone);
        maps.put("piccode", captcha);
        JSONObject jsonData = Constants.getJsonData(maps, null);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getSmsCode(requestBody), httpRespond -> getView().sendSMSRes(httpRespond));
    }

    public void checkCaptcha(String phone, String captcha) {
        Map<String, Object> maps = new HashMap<>();
        maps.put("mobile", phone);
        maps.put("code", captcha);
        JSONObject jsonData = Constants.getJsonData(maps, null);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().checkCaptcha(requestBody), httpRespond -> getView().checkCaptchaRes(httpRespond));

    }
}